package com.igoal.dto;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.igoal.entity.UserGoal;
import com.igoal.entity.UserGoalEntry;
import com.igoal.service.paging.PageItem;
import com.igoal.utils.CalendarUtils;
import com.igoal.utils.SynchronizeAttributeWithOther;
import com.igoal.utils.SynchronizeDirection;

public class UserGoalEntryDTO extends BaseDTO<UserGoalEntry> implements
		Serializable, PageItem<Long>, Commentable {
	private static final long serialVersionUID = 3275372123287425294L;

	private String title;
	private String body;
	private String imgURL;
	private boolean block;
	private boolean permission;
	private Integer commentCount;
	private Integer cheerCount;
	private Long ownerId;
	private String ownerName;
	private String ownerAvatar;
	private Long goalId;
	private String goalTitle;
	private Integer likeCount;
	private List<CommentDTO> comments;
	private int entryType = 1;//1 is entry; 2 is entry done
	private Long sortedValue; // createDate
	private Date goalCreatedDate;
	
	public UserGoalEntryDTO() {}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getImgURL() {
		return imgURL;
	}

	public void setImgURL(String imgURL) {
		this.imgURL = imgURL;
	}

	public boolean getBlock() {
		return block;
	}

	public boolean getPermission() {
		return permission;
	}

	public void setPermission(boolean permission) {
		this.permission = permission;
	}

	public void setBlock(boolean block) {
		this.block = block;
	}

	public Integer getCommentCount() {
		return commentCount;
	}

	public void setCommentCount(Integer commentCount) {
		this.commentCount = commentCount;
	}

	public Integer getCheerCount() {
		return cheerCount;
	}

	public void setCheerCount(Integer cheerCount) {
		this.cheerCount = cheerCount;
	}

	public Long getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(Long ownerId) {
		this.ownerId = ownerId;
	}

	public String getOwnerName() {
		return ownerName;
	}

	public void setOwnerName(String ownerName) {
		this.ownerName = ownerName;
	}

	public Long getGoalId() {
		return goalId;
	}

	public void setGoalId(Long goalId) {
		this.goalId = goalId;
	}

	public String getGoalTitle() {
		return goalTitle;
	}

	public void setGoalTitle(String goalTitle) {
		this.goalTitle = goalTitle;
	}

	public Integer getLikeCount() {
		return likeCount;
	}

	public void setLikeCount(Integer likeCount) {
		this.likeCount = likeCount;
	}
//
//	public List<CommentEntryDTO> getComments() {
//		return comments;
//	}
//
//	public void setComments(List<CommentEntryDTO> comments) {
//		this.comments = comments;
//	}

	public String getOwnerAvatar() {
		return ownerAvatar;
	}

	public void setOwnerAvatar(String ownerAvatar) {
		this.ownerAvatar = ownerAvatar;
	}

	public int getEntryType() {
		return entryType;
	}

	public void setYouLikeThis(boolean likeThis) {
		this.isYouLikeThis = likeThis;
	}

	public boolean getYouLikeThis() {
		return this.isYouLikeThis;
	}
	
	public void setYouCheerThis(boolean cheerThis) {
		this.isYouCheerThis = cheerThis;
	}

	public boolean getYouCheerThis() {
		return this.isYouCheerThis;
	}
	
	public Long getSortedValue() {
		return sortedValue;
	}
	
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "userGoal")
	public void initUserGoalFromEntity(Object entity) {
		if (entity != null && entity instanceof UserGoal) {
			UserGoal userGoal = (UserGoal) entity;
			this.ownerId = userGoal.getUser().getId();
			this.ownerName = userGoal.getUser().getUsername();
			this.ownerAvatar = userGoal.getUser().getAvatar();
			this.goalId = userGoal.getGoal().getId();
			this.goalTitle = userGoal.getGoal().getTitle();
			this.goalCreatedDate = userGoal.getCreateDate();
		}
	}

//	@SuppressWarnings("unchecked")
//	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "comments")
//	public void initCommentsFromEntity(Object comments) {
//		if (comments != null) {
//			Set<CommentEntry> data = (Set<CommentEntry>) comments;
//			List<CommentEntry> entries = new ArrayList<CommentEntry>(data);
//			CommentEntryComparator comparator = new CommentEntryComparator(
//					true);
//			Collections.sort(entries, comparator);
//			this.comments = new ArrayList<CommentEntryDTO>();
//			for (CommentEntry comment : entries) {
//				if (DataStatus.DELETED.equals(comment.getStatus()))
//					continue;
//				CommentEntryDTO dto = new CommentEntryDTO();
//				dto.initFrom(comment);
//				this.comments.add(dto);
//			}
//		}
//	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "updateDate")
	public void initUpdatedSinceFromEntity(Object entity) {
		if (entity != null && entity instanceof Date) {
			Date date = (Date) entity;
			this.updatedSince = CalendarUtils.calculateMinutesDifferentBeforeNow(date);
			this.updateDate = date.getTime();
		}
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "createDate")
	public void initCreatedDateFromEntity(Object entity) {
		if (entity != null && entity instanceof Date) {
			Date date = (Date) entity;
			transformCreatedDate(date);
			sortedValue = date.getTime();
		}
	}

	public void setComments(List<CommentDTO> comments) {
		this.comments = comments;
	}

	public List<CommentDTO> getComments() {
		return this.comments;
	}

	public Date getGoalCreatedDate() {
		return goalCreatedDate;
	}

	public void setGoalCreatedDate(Date goalCreatedDate) {
		this.goalCreatedDate = goalCreatedDate;
	}
}
